package test.service.contacts;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class testAppListContentProvider extends ContentProvider {
	private String TAG = "testAppListContentProvider";
	
	private static final UriMatcher m_URIMatcher  = buildUriMatcher();
	private static final String TABLE_NAME = "testTable";
	private static final String DBNameStr = "testContactsDB.db";
	private static final String CONTENT_ANY_TYPE = "test.content.providers.testprovider/test.any.type";
	
	public static final String AUTHORITY = "test.content.providers.testprovider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
	
	public static final String APP_NAME = "_app_name";
	public static final String IS_ENABLE = "_is_enable";
	public static final String INIT_DATE = "_init_date";
	
	private static final int TABLE_INDICATOR = 1;
	private SQL_Helper m_oneDatabase;
	
	private static  UriMatcher buildUriMatcher()
	{
		UriMatcher matcher  = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(AUTHORITY,
		TABLE_NAME,
		TABLE_INDICATOR);
		return  matcher;
	}
	
	private class SQL_Helper extends SQLiteOpenHelper{
	  public SQL_Helper(Context context) {
	   super(context, DBNameStr, null, 1);
	   // TODO Auto-generated constructor stub
	   
	   
	  }
	  @Override
	  public void onCreate(SQLiteDatabase db) {
	   
	   // TODO Auto-generated method stub
		  db.execSQL("create table IF NOT EXISTS 'testTable'(" + 
					"_id integer primary key autoincrement, " + 
					"_app_name text not null, " + 
					"_is_enable  CHAR(1) CHECK (_is_enable IN ( 'Y', 'N' ))," + 
					"_init_date text not null " + 
					")");
	  }
	  @Override
	  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	   // TODO Auto-generated method stub
		  db.execSQL("DROP TABLE IF EXISTS testTable");
	  }
	  
	 }

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return CONTENT_ANY_TYPE;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		Log.d(TAG, "test insert");
		if(m_URIMatcher.match(uri) != TABLE_INDICATOR)
			return null;
		Uri insertUri = null;
		if(!values.containsKey(APP_NAME))
			return null;
		if(!values.containsKey(IS_ENABLE) || values.getAsString(IS_ENABLE).equals(null))
			values.put(IS_ENABLE, "N");
		if(!values.containsKey(INIT_DATE)){
			Long currentTime = Long.valueOf(System.currentTimeMillis());
			values.put(INIT_DATE, currentTime);
		}
		SQLiteDatabase db = m_oneDatabase.getReadableDatabase();
		long rowID = db.insert(TABLE_NAME, null, values);
		if(rowID > 0){
			insertUri = ContentUris.withAppendedId(CONTENT_URI, rowID);
			getContext().getContentResolver().notifyChange(insertUri, null);
		}
		
		return insertUri;
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		Log.d(TAG, "test onCreate");
		m_oneDatabase = new SQL_Helper(getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		Log.d(TAG, "test query");
		if(m_URIMatcher.match(uri) == TABLE_INDICATOR){
			SQLiteDatabase db = m_oneDatabase.getReadableDatabase();
			Cursor cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
			
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			return cursor;
		}
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
